// Copyright Epic Games, Inc. All Rights Reserved.

#include "LyraAbilityCost_InventoryItem.h"
#include "GameplayAbilitySpec.h"
#include "GameplayAbilitySpecHandle.h"

#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraAbilityCost_InventoryItem)

/**
 * 构造函数，初始化数量默认值为1.0
 */
ULyraAbilityCost_InventoryItem::ULyraAbilityCost_InventoryItem()
{
	Quantity.SetValue(1.0f); //设置数量值为1.0
}

/**
 * 检查是否能支付成本（当前始终返回false）
 * @param Ability 游戏能力
 * @param Handle 能力规格句柄
 * @param ActorInfo 演员信息
 * @param OptionalRelevantTags 可选相关标签容器
 * @return 当前始终返回false
 */
bool ULyraAbilityCost_InventoryItem::CheckCost(const ULyraGameplayAbility* Ability, const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo* ActorInfo, FGameplayTagContainer* OptionalRelevantTags) const
{
#if 0 //条件编译，当前代码被禁用
	if (AController* PC = Ability->GetControllerFromActorInfo()) //获取控制器
	{
		if (ULyraInventoryManagerComponent* InventoryComponent = PC->GetComponentByClass<ULyraInventoryManagerComponent>()) //获取库存管理组件
		{
			const int32 AbilityLevel = Ability->GetAbilityLevel(Handle, ActorInfo); //获取能力等级

			const float NumItemsToConsumeReal = Quantity.GetValueAtLevel(AbilityLevel); //根据等级获取要消耗的物品数量（浮点数）
			const int32 NumItemsToConsume = FMath::TruncToInt(NumItemsToConsumeReal); //转换为整数

			return InventoryComponent->GetTotalItemCountByDefinition(ItemDefinition) >= NumItemsToConsume; //检查库存是否足够
		}
	}
#endif
	return false; //当前始终返回false
}

/**
 * 应用成本（消耗物品）- 当前为空实现
 * @param Ability 游戏能力
 * @param Handle 能力规格句柄
 * @param ActorInfo 演员信息
 * @param ActivationInfo 激活信息
 */
void ULyraAbilityCost_InventoryItem::ApplyCost(const ULyraGameplayAbility* Ability, const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilityActivationInfo ActivationInfo)
{
#if 0 //条件编译，当前代码被禁用
	if (ActorInfo->IsNetAuthority()) //检查是否是网络权威
	{
		if (AController* PC = Ability->GetControllerFromActorInfo()) //获取控制器
		{
			if (ULyraInventoryManagerComponent* InventoryComponent = PC->GetComponentByClass<ULyraInventoryManagerComponent>()) //获取库存管理组件
			{
				const int32 AbilityLevel = Ability->GetAbilityLevel(Handle, ActorInfo); //获取能力等级

				const float NumItemsToConsumeReal = Quantity.GetValueAtLevel(AbilityLevel); //根据等级获取要消耗的物品数量（浮点数）
				const int32 NumItemsToConsume = FMath::TruncToInt(NumItemsToConsumeReal); //转换为整数

				InventoryComponent->ConsumeItemsByDefinition(ItemDefinition, NumItemsToConsume); //消耗物品
			}
		}
	}
#endif
}